package com.ysy.tz.mfg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.dao.CarcassInWarehouseDao;
import com.ysy.tz.mfg.dao.GradingDetailDao;
import com.ysy.tz.mfg.dao.ItemDao;
import com.ysy.tz.mfg.entity.CarcassInWarehouseEntity;
import com.ysy.tz.mfg.entity.GradingDetailEntity;
import com.ysy.tz.mfg.entity.ItemEntity;
import com.ysy.tz.mfg.entity.UserEntity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class PigGradingService {
    private HashMap<String, ItemEntity> itemEntityHashMap = new HashMap();

    public PigGradingService() {
        try {
            SQLUtils.executeSQLException(ItemDao.class, (itemDao) -> {
                ItemEntity itemEntity = new ItemEntity();
                itemEntity.setEnterpriseId(((UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER)).getEnterpriseId());
                ArrayList<ItemEntity> itemEntities = itemDao.listForPage(itemEntity);
                Iterator var4 = itemEntities.iterator();

                while(var4.hasNext()) {
                    ItemEntity entity = (ItemEntity)var4.next();
                    this.itemEntityHashMap.put(entity.getCorrespond(), entity);
                }

            });
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public void createGradingRecord(GradingDetailEntity gradingDetailEntity, Integer warehouseId) throws Exception {
        UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
        String workDay = "" + SessionUtils.getAttribute(SessionUtils.WORK_DAY);
        gradingDetailEntity.setEnterpriseId(currentUser.getEnterpriseId());
        gradingDetailEntity.setCreateUserCode(currentUser.getUserCode());
        gradingDetailEntity.setCreateUserName(currentUser.getUserName());
        SQLUtils.executeSQLTransException((sqlSession) -> {
            GradingDetailDao gradingDetailDao = (GradingDetailDao)sqlSession.getMapper(GradingDetailDao.class);
            gradingDetailDao.insertGradingDetail(gradingDetailEntity);
            CarcassInWarehouseEntity carcassInWarehouseEntity = new CarcassInWarehouseEntity();
            carcassInWarehouseEntity.setEnterpriseId(currentUser.getEnterpriseId());
            carcassInWarehouseEntity.setWorkDay(workDay);
            carcassInWarehouseEntity.setWorkFlightsCode(currentUser.getFlightsCode());
            carcassInWarehouseEntity.setWarehouseId(warehouseId);
            carcassInWarehouseEntity.setItemId(this.choiseItemId(gradingDetailEntity));
            carcassInWarehouseEntity.setSupplierId(gradingDetailEntity.getSupplierId());
            carcassInWarehouseEntity.setBarcode(gradingDetailEntity.getBarcode());
            carcassInWarehouseEntity.setBillNo(gradingDetailEntity.getBillNo());
            carcassInWarehouseEntity.setLevelCode(gradingDetailEntity.getLevelCode());
            carcassInWarehouseEntity.setSlaughterType(gradingDetailEntity.getSlaughterType());
            carcassInWarehouseEntity.setGender(gradingDetailEntity.getGender());
            carcassInWarehouseEntity.setInvqty(Integer.parseInt("1"));
            carcassInWarehouseEntity.setWeight(gradingDetailEntity.getWeight());
            carcassInWarehouseEntity.setCreateUserCode(currentUser.getUserCode());
            carcassInWarehouseEntity.setCreateUserName(currentUser.getUserName());
            carcassInWarehouseEntity.setMjType(gradingDetailEntity.getMjType());
            CarcassInWarehouseDao carcassInWarehouseDao = (CarcassInWarehouseDao)sqlSession.getMapper(CarcassInWarehouseDao.class);
            carcassInWarehouseDao.save(carcassInWarehouseEntity);
        });
    }

    private Integer choiseItemId(GradingDetailEntity gradingDetailEntity) {
        String correspond = gradingDetailEntity.getSlaughterType() + "_" + gradingDetailEntity.getGender() + "_" + gradingDetailEntity.getMjType() + "_" + gradingDetailEntity.getLevelCode();
        if (gradingDetailEntity.getLevelCode().equals("Y") || gradingDetailEntity.getLevelCode().equals("J")) {
            correspond = "*_*_*_" + gradingDetailEntity.getLevelCode();
        }

        return ((ItemEntity)this.itemEntityHashMap.get(correspond)).getItemId();
    }

    public void updateGradingRecord(GradingDetailEntity gradingDetailEntity) throws Exception {
        UserEntity loginUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
        gradingDetailEntity.setEnterpriseId(loginUser.getEnterpriseId());
        SQLUtils.executeSQLTransException((sqlSession) -> {
            GradingDetailDao gradingDetailDao = (GradingDetailDao)sqlSession.getMapper(GradingDetailDao.class);
            gradingDetailDao.updateGradingDetail(gradingDetailEntity);
            CarcassInWarehouseEntity carcassInWarehouseEntity = new CarcassInWarehouseEntity();
            carcassInWarehouseEntity.setEnterpriseId(loginUser.getEnterpriseId());
            carcassInWarehouseEntity.setBillNo(gradingDetailEntity.getBillNo());
            carcassInWarehouseEntity.setLevelCode(gradingDetailEntity.getLevelCode());
            carcassInWarehouseEntity.setItemId(this.choiseItemId(gradingDetailEntity));
            CarcassInWarehouseDao carcassInWarehouseDao = (CarcassInWarehouseDao)sqlSession.getMapper(CarcassInWarehouseDao.class);
            carcassInWarehouseDao.updateCarcassInWarehouse(carcassInWarehouseEntity);
        });
    }

    public PageInfo<GradingDetailEntity> searchGradingDetailRecords(GradingDetailEntity condition) {
        return (PageInfo)SQLUtils.executeSQLWithReturn(GradingDetailDao.class, (gradingDetailDao) -> {
            PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
            UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
            condition.setEnterpriseId(currentUser.getEnterpriseId());

            try {
                List GradingDetailEntity = gradingDetailDao.listForPage(condition);
                PageInfo<GradingDetailEntity> result = new PageInfo(GradingDetailEntity);
                return result;
            } catch (Exception var5) {
                var5.printStackTrace();
                return null;
            }
        });
    }
}
